package com.begye.smartloan.apptest.gongong

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.begye.smartloan.apptest.MyApplication
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.disclosure.adapter.DisclosurePagerAdapter
import com.begye.smartloan.apptest.tool.IApiProcessCallback
import com.begye.smartloan.apptest.tool.SysConfigTool
import com.begye.smartloan.apptest.tool.doPostRequest
import com.begye.smartloan.apptest.tool.doZoneRequest
import org.json.JSONArray
import org.json.JSONObject

class SysConfigViewModel : ViewModel() {

    private val _serverSuccessfulLiveData = MutableLiveData<String>()
    val onServerSuccessfulLiveData get() = _serverSuccessfulLiveData

    private val _sysConfigSuccessfulLiveData = MutableLiveData<JSONObject>()
    val onSysConfigSuccessfulLiveData get() = _sysConfigSuccessfulLiveData

    private val _sysConfigErrorLiveData = MutableLiveData<Boolean>()
    val onSysConfigErrorLiveData get() = _sysConfigErrorLiveData

    private val _loadingStateLiveData = MutableLiveData<Boolean>()
    val onLoadingStateLiveData get() = _loadingStateLiveData

    fun configPermissionAdapterItems(permissions: List<String>): List<JSONObject> {
        println("permissions:$permissions")
        val list = mutableListOf<JSONObject>()

        if (permissions.isNotEmpty()) {
            val obj = JSONObject()
            obj.put("element", "")
            obj.put("result", "")
            obj.put("type", DisclosurePagerAdapter.CATEGORY_APP)
            obj.put("title", MyApplication.context.getString(R.string.disclosure_text_29))
            obj.put("resId", R.drawable.dres_65)

            val array = JSONArray()
            val obj1 = JSONObject().apply {
                put("title", MyApplication.context.getString(R.string.disclosure_text_19))
                put("detail", MyApplication.context.getString(R.string.disclosure_text_20))
            }
            array.put(obj1)

            val obj2 = JSONObject().apply {
                put("title", MyApplication.context.getString(R.string.disclosure_text_21))
                put("detail", MyApplication.context.getString(R.string.disclosure_text_22))
            }
            array.put(obj2)

            val obj3 = JSONObject().apply {
                put("title", MyApplication.context.getString(R.string.disclosure_text_23))
                put("detail", MyApplication.context.getString(R.string.disclosure_text_24))
            }
            array.put(obj3)

            val obj4 = JSONObject().apply {
                put("title", MyApplication.context.getString(R.string.disclosure_text_25))
                put("detail", MyApplication.context.getString(R.string.disclosure_text_26))
            }
            array.put(obj4)

            obj.put("list", array)
            list.add(obj)
        }

        for (element in permissions) {
            val obj = JSONObject()
            obj.put("element", element)
            obj.put("result", "")

            if (element.contains("READ_SMS", true)) {
                obj.put("type", DisclosurePagerAdapter.CATEGORY_SMS)
                obj.put("title", MyApplication.context.getString(R.string.disclosure_text_27))
                obj.put("resId", R.drawable.dres_13)

                val array = JSONArray()
                val obj1 = JSONObject().apply {
                    put("title", MyApplication.context.getString(R.string.disclosure_text_12))
                    put("detail", MyApplication.context.getString(R.string.disclosure_text_13))
                }
                array.put(obj1)

                val obj2 = JSONObject().apply {
                    put("title", MyApplication.context.getString(R.string.disclosure_text_14))
                    put("detail", MyApplication.context.getString(R.string.disclosure_text_15))
                }
                array.put(obj2)

                val obj3 = JSONObject().apply {
                    put("title", MyApplication.context.getString(R.string.disclosure_text_16))
                    put("detail", MyApplication.context.getString(R.string.disclosure_text_17))
                }
                array.put(obj3)

                obj.put("list", array)

            } else if (element.contains("READ_PHONE_STATE", true)) {
                obj.put("type", DisclosurePagerAdapter.CATEGORY_PHONE)
                obj.put("title", MyApplication.context.getString(R.string.disclosure_text_28))
                obj.put("resId", R.drawable.dres_14)

                val array = JSONArray()
                val obj1 = JSONObject().apply {
                    put("title", "")
                    put("detail", MyApplication.context.getString(R.string.disclosure_text_18))
                }
                array.put(obj1)

                obj.put("list", array)

            }
            list.add(obj)
        }

        return list
    }

    fun configIndicatorAdapterItems(permissions: List<String>): List<JSONObject> {
        println("permissions:$permissions")
        val list = mutableListOf<JSONObject>()

        if (permissions.isNotEmpty()) {
            val obj = JSONObject()
            obj.put("isChecked", true)
            list.add(obj)
        }

        for (index in permissions.indices) {
            val obj = JSONObject()
            obj.put("isChecked", false)
            list.add(obj)
        }

        return list
    }

    fun requestServer() {
        doZoneRequest(
            object : IApiProcessCallback {
                override fun onApiReady() {
                }

                override fun onApiDone(jsonObject: JSONObject) {
                    val data = jsonObject.optString("data", "")
                    if (data.contains(",", true)) {
                        val list = data.split(",")
                        var server = ""
                        var pCode = ""
                        if (list.size > 1) {
                            pCode = list[1].trim()
                        }
                        if (list.isNotEmpty()) {
                            server = list[0].trim()
                        }

                        MyApplication.instance.initDataLibs(server, pCode)
                    } else {
                        MyApplication.instance.initDataLibs(data, "")
                    }

                    _serverSuccessfulLiveData.value = data
                }

                override fun onApiError(code: String, errMsg: String) {
                    _serverSuccessfulLiveData.value = ""
                }
            })
    }

    fun requestSystemConfiguration() {
        doPostRequest(
            path = "/DkU9PmA1k719Fpz9T2u3Pw",
            action = hashMapOf(),
            loading = _loadingStateLiveData,
            callback = object : IApiProcessCallback {
                override fun onApiReady() {
                }

                override fun onApiDone(jsonObject: JSONObject) {
                    try {
                        val obj = jsonObject.optJSONObject("obj") ?: JSONObject()
                        SysConfigTool.updateSysConfig(obj)

                        _sysConfigSuccessfulLiveData.value = obj
                        _sysConfigErrorLiveData.value = false

                    } catch (e: Exception) {
                        //
                        _sysConfigErrorLiveData.value = true
                    }
                }

                override fun onApiError(code: String, errMsg: String) {
                    _sysConfigErrorLiveData.value = true
                }

            })
    }
}